from sqlalchemy import *
from core import log
from core.db import *

class Section(object):
	def __repr__(self):
		return "%d(%d,%d):%s" % (self.id,self.parent_id,self._index,self.name)

	def completeTree(self):
		r = self.__repr__() + "\n"
		for c in self.childs:
			r = r + c.completeTree()
		return r	

	def completePath(self):
		r = "/%s" % self.name
		if self.parent_id!=-1:
			r = self.parent().completePath() + r
		return r	
	
	def file_path(self):
		return "C:/data/section/%d.htm" % self.id

	def parent(self):
		if self.parent_id!=-1:
			return section_query.get(self.parent_id)
		else:
			return None


class Document(object):
	def __repr__(self):
		return "(%d,%s):%s @ %s" % (self.id,self.section.completePath(),self.name,self.path)


class DocumentTitle(object):
	def __repr__(self):
		return "(%s,%s):(%d,%d):%s" % (self.document.name,self.section.completePath(),self.start_pos,self.end_pos,self.comment)


#db = create_engine('mysql://root@localhost:3038/quranResearch')
#metadata = BoundMetaData(db)
#metadata.engine.echo = True

session = create_session()
tsection = Table('section',metadata,
	Column('id',Integer,primary_key=True),
	Column('parent_id',Integer,ForeignKey("section.id")),
	Column('name',Unicode(65536)),
	Column('_index',Integer))
tdocument = Table('document',metadata,
	Column('id',Integer,primary_key=True),
	Column('name',Unicode(65536)),
	Column('path',Unicode(65536)),
	Column('section_id',Integer,ForeignKey("section.id")))
tsection_document = Table('section_document',metadata,
	Column('id',Integer,primary_key=True),
	Column('section_id',Integer,ForeignKey("section.id")),
	Column('document_id',Integer,ForeignKey("document.id")),
	Column('start_pos',Integer),
	Column('end_pos',Integer),
	Column('comment',Unicode(65536)))

zmap = mapper(DocumentTitle,tsection_document)

ymap = mapper(Document,tdocument,properties={
	'section':relation(Section),
	'titles':relation(DocumentTitle)})

xmap = mapper(Section,tsection,properties={
	'childs':relation(Section,cascade="all"),
	'documents':relation(Document),
	'titles':relation(DocumentTitle)},order_by=[tsection.c.parent_id,tsection.c._index])

zmap.add_property('section',relation(Section))
zmap.add_property('document',relation(Document))

section_query = session.query(Section)
document_query = session.query(Document)
documentTitle_query = session.query(DocumentTitle)

#Test Case
if __name__=="__main__":
	initialize()
	s = section_query.select(Section.c.id==139,order_by=[Section.c.parent_id,Section.c._index])[0]

	print "----------------s"
	print s
	print "----------------s.parent()"
	print s.parent()
	print "----------------s.completeTree()"
	print s.completeTree()
	print "----------------s.childs[0].completePath()"
	print s.childs[0].completePath()
	print "----------------s.parent().documents"
	print s.parent().documents
	print "----------------s.parent().documents[0].section"
	print s.parent().documents[0].section
	print "----------------s.parent().documents[0].titles"
	print s.parent().documents[0].titles 
	print "----------------s.parent().document[0].titles[0].section.titles"
	print s.parent().documents[0].titles[0].section.titles
